/****************************************************
	文件：RefCounter.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/12/20 18:13:29
	功能：引用计数器
*****************************************************/

using HTFW.Core;

namespace HTFW.Utility.Global
{
    public abstract class RefCounter : IRefCount
    {
        private int mRefCount;

        public int RefCount => mRefCount;

        /// <summary>
        /// 增加引用计数
        /// </summary>
        /// <param name="num"></param>
        public void IncreaseRef(int num = 1)
        {
            mRefCount += num;
        }

        /// <summary>
        /// 减少引用计数
        /// </summary>
        /// <param name="num"></param>
        public void DecreaseRef(int num = 1)
        {
            mRefCount -= num;
            if (mRefCount <= 0)
            {
                OnRefZero();
                mRefCount = 0;
            }
        }

        /// <summary>
        /// 引用计数为 0 时执行的逻辑
        /// </summary>
        public abstract void OnRefZero();

        /// <summary>
        /// 重置引用计数
        /// </summary>
        public void ResetRefCount()
        {
            mRefCount = 0;
        }
    }
}